﻿namespace EasyWeb
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal static partial class EWGuard
    {
        public static void IsNotDefault<T>(T parameter, string parameterName)
        {
            if (object.Equals(parameter, default(T)))
            {
                throw new ArgumentException(string.Format("The parameter {0} cannot be the default value of type {1}.", parameterName, typeof(T)), parameterName);
            }
        }

        public static void IsNotNull(object parameter, string parameterName)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(parameterName, string.Format("The parameter {0} cannot be null.", parameterName));
            }
        }

        public static void IsNotNullOrEmpty(string parameter, string parameterName)
        {
            if (string.IsNullOrEmpty(parameter))
            {
                throw new ArgumentException(string.Format("The parameter {0} cannot be null or empty.", parameterName), parameterName);
            }
        }

        public static void IsNotNullOrEmpty<T>(IEnumerable<T> parameter, string parameterName)
        {
            EWGuard.IsNotNull(parameter, parameterName);
            if (parameter.Count() == 0)
            {
                throw new ArgumentException(string.Format("The collection {0} cannot be null or empty.", parameterName), parameterName);
            }
        }

        public static void IsNotNullOrWhiteSpace(string parameter, string parameterName)
        {
            if (string.IsNullOrWhiteSpace(parameter))
            {
                throw new ArgumentException(string.Format("The parameter {0} cannot be null or white space.", parameterName), parameterName);
            }
        }

        public static void IsDefinedEnum<T>(T parameter, string parameterName)
        {
            if (!Enum.IsDefined(typeof(T), parameter))
            {
                throw new ArgumentException(string.Format("The value {0} is not defined in the enumeration type {1}.", parameter, typeof(T).FullName), "parameter");
            }
        }

        public static void IsNotNegative(int parameter, string parameterName)
        {
            if (parameter < 0)
            {
                throw new ArgumentOutOfRangeException(parameterName, string.Format("The parameter {0} cannot be negative.", parameterName));
            }
        }

        public static void IsNotZeroOrNegative(int parameter, string parameterName)
        {
            if (parameter <= 0)
            {
                throw new ArgumentOutOfRangeException(parameterName, string.Format("The parameter {0} cannot be zero or negative.", parameterName));
            }
        }
    }
}